local skill = fk.CreateSkill {
  name = "crossing_the_river_in_white_skill",
}

Fk:loadTranslationTable{
  ["crossing_the_river_in_white_draw"] = "摸牌至最多",
  ["crossing_the_river_in_white_throw"] = "弃牌至最少",
}

skill:addEffect("cardskill", {
  prompt = "#crossing_the_river_in_white_skill",
  mod_target_filter = function(self, player, to_select, selected, card)
    return to_select.role == "lord"
  end,
  can_use = function(self, player, card)
    local lord = table.filter(Fk:currentRoom().alive_players, function (p)
        return p.role == "lord"
      end)
    return not player:prohibitUse(card) and not player:isProhibited(lord[1], card)
  end,
  on_use = function(self, room, use)
    if not use.tos or #use.tos == 0 then
      local player = use.from
      local lord = table.filter(room.alive_players, function (p)
        return p.role == "lord"
      end)
      use.tos = { lord[1] }
    end
  end,
  on_effect = function(self, room, effect)
    local from = effect.from
    local to = effect.to
    if to.dead then return end
    local all_choices = {"crossing_the_river_in_white_draw", "crossing_the_river_in_white_throw"}
    local choices = table.clone(all_choices)
    local max = 0
    local min = 999
    for _, p in ipairs(room.alive_players) do
      if max < p:getHandcardNum() then
        max = p:getHandcardNum()
      end
      if min > p:getHandcardNum() then
        min = p:getHandcardNum()
      end
    end
    local choice = room:askToChoice(from, {
      choices = choices,
      skill_name = skill.name,
      all_choices = all_choices,
    })
    if choice == "crossing_the_river_in_white_draw" then
      local n = max - to:getHandcardNum()
      if n > 0 then
        to:drawCards(math.min(n, 20), skill.name)
      end
    else
      local n = to:getHandcardNum() - min
      if n > 0 then
        room:askToDiscard(to, {
        min_num = n,
        max_num = n,
        include_equip = false,
        skill_name = skill.name,
        cancelable = false,
      })
      end
    end
  end,
})

return skill

